<?php

namespace app\index\model;

use think\facade\Db;
use app\common\model\Common;

class UserIp extends Common
{
    /**
     * 返回layui的table所需要的格式
     * @param $post
     * @param $user_id
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function tableData($post)
    {
        $list_rows  = $post['limit'] ?? config('paginate.list_rows');
        $page       = $post['page'] ?? config('paginate.page');

        $where =[];
        $where[] = ['user_id', '=', $post['user_id']];
        if(isset($post['ip']) && !empty($post['ip'])){
            $where[] = ['ip', 'like', '%'.$post['ip'].'%'];
        }
        $list = Db::name('user_ip')
                ->where($where)
                ->order('ctime desc')
                ->paginate(['list_rows' => $list_rows, 'page' => $page])
                ->toArray();

        return $result = [
            'code'  => 0,
            'msg'   => lang('get info success'),
            'data'  => $list['data'],
            'count' => $list['total'],
        ];
    }

    public function depDel($data)
    {
        $user_id        = $data['user_id'];
        $user_ip_list   = $this->find($data['id']);
        $ip             = $user_ip_list['ip'];
        $re             = $user_ip_list->delete();

        //记录用户日志
        $userLog = new UserLog();
        $userLog->setLog($user_id , 'delete Device Ip' , ["del Ip" => $ip]);

        return $re;
    }
}